package de.dfki.asr.genesis2.sms.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Locale;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class YoutubePOIXMLExtractor extends SocialPOIXMLExtractor{

	private static final String source = "youtube";
	private static final String youtubeDateFormat = "yyyy-mm-dd'T'HH:mm:ss.SSS'Z'";
	private static final SimpleDateFormat youtubeFormatter = new SimpleDateFormat(youtubeDateFormat, Locale.ENGLISH);
	
	

	@Override
	public String extractPOIXMLsFromResponse(String body, int maxResults) {
    	String pois = "";
    	
    	SocialPOIXMLExtractor poiExtractor = new YoutubePOIXMLExtractor();
    	try {
			JSONObject jobj = new JSONObject(body);
			JSONArray results = jobj.getJSONObject("feed").getJSONArray("entry");
			maxResults = Math.min(maxResults, results.length());
			for (int i=0; i<results.length() && i<maxResults; i++) {
				JSONObject o = results.getJSONObject(i);
				pois += poiExtractor.extractPOIXML(o)  + "\n";
			}
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
		}

    	return pois;
	}

	
	@Override
	public String extractId(JSONObject jsonObj) {
		try {
			String id = jsonObj.getJSONObject("id").getString("$t");
			return id.replaceFirst("http://gdata.youtube.com/feeds/api/videos/", "");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public Point extractPoint(JSONObject jsonObj) {
		try {
			String  coord = jsonObj.getJSONObject("georss$where")
								   .getJSONObject("gml$Point")
								   .getJSONObject("gml$pos")
								   .getString("$t");
			String latlon[] = coord.split(" ");
			if (latlon.length >= 2) {
				double lat = Double.parseDouble(latlon[0]);
				double lon = Double.parseDouble(latlon[1]);
				return new Point(lat,lon);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
		}
		return null;
	}

	@Override
	public Location extractLocation(JSONObject jsonObj) {
		Point point = extractPoint(jsonObj);
		Address address = extractAddress(jsonObj);
		if (point != null)
			return new Location(point, address);
		return null;
	}

	private Address extractAddress(JSONObject jsonObj) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public String extractLabel(JSONObject jsonObj) {
		try {
			String msg = jsonObj.getJSONObject("title")
								.getString("$t");
			if (!msg.equals(""))
				return msg;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public String extractMessage(JSONObject jsonObj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String extractDescription(JSONObject jsonObj) {
		try {
			String msg = jsonObj.getJSONObject("media$group")
								.getJSONObject("media$description")
								.getString("$t");
			if (!msg.equals(""))
				return msg;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public User extractFrom(JSONObject jsonObj) {
		try {
			jsonObj = jsonObj.getJSONArray("author").getJSONObject(0);
			String username = jsonObj.getJSONObject("name").getString("$t");
			String url = "http://www.youtube.com/users/" + username;
			User user = new User();
			user.setUsername(username);
			user.setLink(new Link(url,"text/html"));
			return user;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public ArrayList<User> extractToList(JSONObject jsonObj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ArrayList<TaggedUser> extractTagList(JSONObject jsonObj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String extractKeywords(JSONObject jsonObj) {
		try {
			String msg = jsonObj.getJSONObject("media$group")
								.getJSONObject("media$keywords")
								.getString("$t");
			if (!msg.equals(""))
				return msg;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
		}
		return null;
	}

	@Override
	public String extractUrl(JSONObject jsonObj) {
		try {
			jsonObj = jsonObj.getJSONArray("link").getJSONObject(0);
			return jsonObj.getString("href");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public String extractAction(JSONObject jsonObj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long extractTimestamp(JSONObject jsonObj) {
		try {
			jsonObj = jsonObj.getJSONObject("published");
			return youtubeFormatter.parse(jsonObj.getString("$t")).getTime();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}

	@SuppressWarnings("static-access")
	@Override
	public String extractSource(JSONObject jsonObj) {
		return this.source;
	}


	@Override
	public Media extractMediaContent(JSONObject jsonObj) {
		try {
			jsonObj = jsonObj.getJSONObject("media$group").getJSONArray("media$content").getJSONObject(0);
			String url  = jsonObj.getString("url");
			String type = jsonObj.getString("type");
			Media media = new Media(url, type);
			media.setDuration(jsonObj.getInt("duration"));
			return media;
				
		} catch (JSONException e) {
			// TODO Auto-generated catch block
		}
		return null;
	}

	@Override
	public Media extractMediaThumbnail(JSONObject jsonObj) {
		try {
			jsonObj = jsonObj.getJSONObject("media$group").getJSONArray("media$thumbnail").getJSONObject(0);
			String url = jsonObj.getString("url");	
			String type ="image/jpeg";
			Media media = new Media(url, type);
			media.setHeight(jsonObj.getInt("height"));
			media.setWidth(jsonObj.getInt("width"));
			return media;
				
		} catch (JSONException e) {
			// TODO Auto-generated catch block
		}
		return null;
	}

	@Override
	public Link extractLink(JSONObject jsonObj) {
		try {
			String url = jsonObj.getJSONArray("link").getJSONObject(0).getString("href");
			return new Link(url, "text/html");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}


}
